package com.hw.paltfrom.service.system;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hw.paltfrom.mapper.HwBaseAuthorizeMapper;
import com.hw.paltfrom.mapper.HwBaseRoleMapper;
import com.hw.paltfrom.mapper.HwBaseUserRelationMapper;
import com.hw.paltfrom.pojo.HwBaseAuthorize;
import com.hw.paltfrom.pojo.HwBaseRole;
import com.hw.paltfrom.pojo.HwBaseUserRelation;
import com.hw.paltfrom.rescode.HWSystemCode;
import com.hw.paltfrom.utils.RandomNumberUtil;
import com.hw.paltfrom.utils.ResponeResultUtil;
import com.hw.paltfrom.vo.BaseRoleVo;

@Service
public class BaseRoleServer {

	@Autowired
	private HwBaseRoleMapper baseRoleMapper;
	
	@Autowired
	private HwBaseAuthorizeMapper authorizeMapper;
	
	
	@Autowired
	private HwBaseUserRelationMapper userRelationMapper;

	public Map<String, Object> insert(BaseRoleVo vo) {

		// 先判断分类名称
		Wrapper<HwBaseRole> namewrapper = new EntityWrapper<HwBaseRole>();
		
//		!!!排除删除的
		namewrapper.eq("F_FullName", vo.getfFullname()).eq("F_DeleteMark", "0");
		List<HwBaseRole> HwBaseRolesList = baseRoleMapper.selectList(namewrapper);
		if (!HwBaseRolesList.isEmpty()) {
			return ResponeResultUtil.fail(HWSystemCode.ROLENAME_EXIT);
		}

		// 先判断 编码是否重复
//		Wrapper<HwBaseRole> codewrapper = new EntityWrapper<HwBaseRole>();
//		codewrapper.eq("F_EnCode", vo.getfEncode());
//		List<HwBaseRole> codeList = baseRoleMapper.selectList(codewrapper);
//		if (!codeList.isEmpty()) {
//			return ResponeResultUtil.fail(HWSystemCode.ROLECODE_EXIT);
//		}

		HwBaseRole hwBaseRole = new HwBaseRole();
		hwBaseRole.setfRoleid(RandomNumberUtil.getNumber());
		hwBaseRole.setfCategory("1");
		hwBaseRole.setfEncode(RandomNumberUtil.getCode());
		hwBaseRole.setfFullname(vo.getfFullname());
		hwBaseRole.setfSortcode(vo.getfSortcode());
		hwBaseRole.setfDeletemark(0);
		hwBaseRole.setfEnabledmark(1);
		hwBaseRole.setfDescription(vo.getfDescription());
		hwBaseRole.setfCreatedate(new Timestamp(System.currentTimeMillis()));
		hwBaseRole.setfCreateuserid(vo.getfCreateuserid());
		hwBaseRole.setfCreateusername(vo.getfCreateusername());
		Integer insert = baseRoleMapper.insert(hwBaseRole);
		if (insert != 1) {
			return ResponeResultUtil.fail(HWSystemCode.FAIL);
		}
		return ResponeResultUtil.success(HWSystemCode.SUCCESS);
	}

	public Map<String, Object> queryAllList(BaseRoleVo vo) {
		
	    PageHelper.startPage(vo.getPageNum(),vo.getPageSize());

		// 模糊查询规则
		Wrapper<HwBaseRole> namewrapper = new EntityWrapper<HwBaseRole>();
		String getfEncode = vo.getfEncode();
		String getfFullname = vo.getfFullname();
		if(StringUtils.isNotBlank(getfEncode)){
			namewrapper.like("F_EnCode", vo.getfEncode());
		}
		if(StringUtils.isNotBlank(getfFullname)){
			namewrapper.like("F_FullName", vo.getfFullname());
		}
		namewrapper.eq("F_DeleteMark", "0").orderBy("F_SortCode");
		
//		if (StringUtils.isNotBlank(vo.getfEncode())) {
//			namewrapper.like("F_EnCode", vo.getfEncode()).and().eq("F_DeleteMark", "0").orderBy("F_SortCode");
//		} else if (StringUtils.isNotBlank(vo.getfFullname())) {
//			namewrapper.like("F_FullName", vo.getfFullname()).and().eq("F_DeleteMark", "0").orderBy("F_SortCode");
//		} else if (!StringUtils.isNotBlank(vo.getfEncode()) && !StringUtils.isNotBlank(vo.getfFullname())) {
//			namewrapper.eq("F_DeleteMark", "0").orderBy("F_SortCode");
//		}
		
		List<HwBaseRole> selectList = baseRoleMapper.selectList(namewrapper);
		PageInfo<HwBaseRole> info = new PageInfo<HwBaseRole>(selectList);
		return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, info);
	}

	public Map<String, Object> update(BaseRoleVo vo ){
		
		HwBaseRole baseRole=new HwBaseRole();
		Wrapper<HwBaseRole> wrapper = new EntityWrapper<HwBaseRole>();
		wrapper.eq("F_RoleId", vo.getfRoleid());
		
//		如果删除标记为1的话，当前是删除操作
		if(vo.getfDeletemark()==1){
			baseRole.setfDeletemark(vo.getfDeletemark());
//			记录是谁删除的
			baseRole.setfModifydate(new Timestamp(System.currentTimeMillis()));
			baseRole.setfModifyuserid(vo.getfModifyuserid());
			baseRole.setfModifyusername(vo.getfModifyusername());
			Integer update = baseRoleMapper.update(baseRole, wrapper);
			if (update != 1) {
				return ResponeResultUtil.fail(HWSystemCode.FAIL);
			}
			
			//删除角色菜单关系
			Wrapper<HwBaseAuthorize> authwrapper=new EntityWrapper<HwBaseAuthorize>();
			authwrapper.eq("F_RoleId", vo.getfRoleid());
			Integer delete = authorizeMapper.delete(authwrapper);
			
//			删除角色用户关系
			Wrapper<HwBaseUserRelation> relawrapper=new EntityWrapper<HwBaseUserRelation>();
			relawrapper.eq("F_RoleId", vo.getfRoleid());
			userRelationMapper.delete(relawrapper);
			
			
		}else{
			Wrapper<HwBaseRole> namewrapper = new EntityWrapper<HwBaseRole>();
			namewrapper.eq("F_FullName", vo.getfFullname()).and().notIn("F_RoleId", vo.getfRoleid());
			List<HwBaseRole> HwBaseRolesList = baseRoleMapper.selectList(namewrapper);
			if (!HwBaseRolesList.isEmpty()) {
				return ResponeResultUtil.fail(HWSystemCode.ROLENAME_EXIT);
			}
			
			
			baseRole.setfFullname(vo.getfFullname());
			baseRole.setfSortcode(vo.getfSortcode());
			baseRole.setfDeletemark(vo.getfDeletemark());
			baseRole.setfEnabledmark(vo.getfEnabledmark());
			baseRole.setfDescription(vo.getfDescription());
			baseRole.setfModifydate(new Timestamp(System.currentTimeMillis()));
			baseRole.setfModifyuserid(vo.getfModifyuserid());
			baseRole.setfModifyusername(vo.getfModifyusername());
			Integer update = baseRoleMapper.update(baseRole, wrapper);
			if (update != 1) {
				return ResponeResultUtil.fail(HWSystemCode.FAIL);
			}
		}
		return ResponeResultUtil.success(HWSystemCode.SUCCESS);
	}
	
}
